package com.yeyks.mattress.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yeyks.common.exception.ServiceException;
import com.yeyks.common.param.MattressPackageConfigInsertParam;
import com.yeyks.common.utils.ObjectConvertUtil;
import com.yeyks.mattress.dal.dao.MattressPackageConfigMapper;
import com.yeyks.mattress.dal.domain.MattressPackageConfig;
import com.yeyks.mattress.service.MattressPackageConfigService;
import com.yeyks.mattress.vo.OrderInfoMattressPackageListVo;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * <p>
 * 共享床垫套餐配置表 服务实现类
 * </p>
 *
 * @author yun
 * @since 2019-07-16
 */
@Service
public class MattressPackageConfigServiceImpl extends ServiceImpl<MattressPackageConfigMapper, MattressPackageConfig> implements MattressPackageConfigService {

    @Override
    public OrderInfoMattressPackageListVo packageList() {
        List<MattressPackageConfig> list = list(new QueryWrapper<MattressPackageConfig>()
                .eq(MattressPackageConfig.STATUS, 1).orderByDesc(MattressPackageConfig.SORT));
        return new OrderInfoMattressPackageListVo().setPackages(list);
    }

    /**
     * 新增套餐
     *
     * @param param
     * @return
     */
    @Override
    public Boolean configInsert(MattressPackageConfigInsertParam param) {
        List<MattressPackageConfig> mattressPackageConfigs = baseMapper.selectList(new QueryWrapper<MattressPackageConfig>()
                .eq(MattressPackageConfig.PACKAGE_NAME, param.getPackageName())
                .notIn(MattressPackageConfig.STATUS, -1));
        if (null != mattressPackageConfigs && mattressPackageConfigs.size() > 0) {
            throw new ServiceException("该套餐已存在");
        }
        MattressPackageConfig packageConfig = ObjectConvertUtil.convertObject(param, MattressPackageConfig.class);
        packageConfig.setSort(0);
        packageConfig.setTimeLong(param.getTimeLong());
        if (1 != baseMapper.insert(packageConfig)) {
            throw new ServiceException("新增套餐异常");
        }
        return true;
    }

    /**
     * 修改套餐
     *
     * @param param
     * @return
     */
    @Override
    public Boolean configUpdate(MattressPackageConfig param) {
        if (null == param.getId()) {
            throw new ServiceException("套餐id必传");
        }
        if (null != param.getTimeLong()) {
            param.setTimeLong(param.getTimeLong());
        }
        return baseMapper.updateById(param) == 1;
    }

    @Override
    public Boolean deleteConfigPackage(Integer id) {
        if (null == id) {
            throw new ServiceException("套餐id必传");
        }
        return baseMapper.update(null, new UpdateWrapper<MattressPackageConfig>()
                .set(MattressPackageConfig.STATUS, -1)
                .eq(MattressPackageConfig.ID, id)) == 1;
    }

}
